package com.bsoft.ecommerce.entity;
import com.alibaba.fastjson.JSON;
import com.bsoft.ecommerce.constant.BrandCategory;
import com.bsoft.ecommerce.constant.GoodsCategory;
import com.bsoft.ecommerce.constant.GoodsStatus;
import com.bsoft.ecommerce.coverter.BrandCategoreConverter;
import com.bsoft.ecommerce.coverter.GoodsCategoryCoverter;
import com.bsoft.ecommerce.goods.GoodsInfo;
import com.bsoft.ecommerce.goods.SimpleGoodsInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_ecommerce_goods")
public class EcommerceGoods {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;
    @Column(name = "good_category", nullable = false)
    @Convert(converter = GoodsCategoryCoverter.class)
    private GoodsCategory goodsCategory;
    @Column(name = "brand_category", nullable = false)
    @Convert(converter = BrandCategoreConverter.class)
    private BrandCategory brandCategory;
    @Column(name = "goods_name", nullable = false)
    private String goodsName;
    @Column(name = "goods_pic", nullable = false)
    private String goodsPic;
    @Column(name = "goods_description", nullable = false)
    private String goodsDescription;
    @Column(name = "good_status", nullable = false)
    @Convert(converter = GoodsCategoryCoverter.class)
    private GoodsStatus goodsStatus;
    @Column(name = "prince", nullable = false)
    private Integer prince;
    @Column(name = "supply", nullable = false)
    private Long supply;
    @Column(name = "inventory", nullable = false)
    private Long inventory;
    @Column(name = "goods_property", nullable = false)
    private String goodsProperty;
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;
    public static EcommerceGoods to(GoodsInfo goodsInfo) {
        EcommerceGoods ecommerceGoods = new EcommerceGoods();
        ecommerceGoods.setGoodsCategory(GoodsCategory.of(goodsInfo.getGoodsCategory()));
        ecommerceGoods.setBrandCategory(BrandCategory.of(goodsInfo.getGoodsCategory()));
        ecommerceGoods.setGoodsName(goodsInfo.getGoodsName());
        ecommerceGoods.setGoodsPic(goodsInfo.getGoodsPic());
        ecommerceGoods.setGoodsDescription(goodsInfo.getGoodsDescription());
        ecommerceGoods.setGoodsStatus(GoodsStatus.ONLINE);
        ecommerceGoods.setPrince(goodsInfo.getPrince());
        ecommerceGoods.setSupply(goodsInfo.getSupply());
        ecommerceGoods.setInventory(goodsInfo.getInventory());
        ecommerceGoods.setGoodsProperty(JSON.toJSONString(goodsInfo.getGoodsProperty()));
        return ecommerceGoods;
    }
    public GoodsInfo toGoodsInfo() {
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(this.id);
        goodsInfo.setGoodsCategory(this.goodsCategory.getCode());
        goodsInfo.setBrandCategory(this.brandCategory.getCode());
        goodsInfo.setGoodsName(this.goodsName);
        goodsInfo.setGoodsPic(this.goodsPic);
        goodsInfo.setGoodsStatus(this.goodsStatus.getStatus());
        goodsInfo.setGoodsDescription(this.goodsDescription);
        goodsInfo.setPrince(this.prince);
        goodsInfo.setGoodsProperty(JSON.parseObject(this.goodsProperty, GoodsInfo.GoodsProperty.class));
        goodsInfo.setSupply(this.supply);
        goodsInfo.setInventory(this.inventory);
        goodsInfo.setCreateTime(this.createTime);
        goodsInfo.setUpdateTime(this.updateTime);
        return goodsInfo;
    }
    public SimpleGoodsInfo toSimple(){
        SimpleGoodsInfo simpleGoodsInfo= new SimpleGoodsInfo();
        simpleGoodsInfo.setId(this.id);
        simpleGoodsInfo.setGoodsPic(this.goodsPic);
        simpleGoodsInfo.setGoodsName(this.goodsName);
        simpleGoodsInfo.setPrince(this.prince);
        return  simpleGoodsInfo;
    }
}
